package demo2lambda;

//函数式接口：当一个接口中，只有一个抽象方法时

import javax.jws.soap.SOAPBinding;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.PriorityQueue;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

//无返回值无参数
@FunctionalInterface
interface NoParameterNoReturn{
    void test();
}

//无返回值一个参数
@FunctionalInterface
interface OneParameterNoReturn {
    void test(int a);
}
//无返回值多个参数
@FunctionalInterface
interface MoreParameterNoReturn {
    void test(int a,int b);
}
//有返回值无参数
@FunctionalInterface
interface NoParameterReturn {
    int test();
}
//有返回值一个参数
@FunctionalInterface
interface OneParameterReturn {
    int test(int a);
}
//有返回值多参数
@FunctionalInterface
interface MoreParameterReturn {
    int test(int a,int b);
}

public class Test {

    public static void main(String[] args) {
        HashMap<String,Integer> map = new HashMap<>();
        map.put("huu",2);
        map.put("kkk",6);
        map.put("www",3);

        /*map.forEach(new BiConsumer<String, Integer>() {
            @Override
            public void accept(String s, Integer integer) {
                System.out.println("key: " + s + "value: "+ integer);
            }
        });*/

        map.forEach((k,v) -> System.out.println("key: " + k + "value: "+ v));

    }

    public static void main6(String[] args) {
        ArrayList<String> list =new ArrayList<>();
        list.add("hello");
        list.add("abcd");
        list.add("hanqing");
        /*list.forEach(new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println(s);
            }
        });*/

        list.forEach(s -> System.out.println(s));
        System.out.println("============");
        /*list.sort(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o1.compareTo(o2);
            }
        });*/
        list.sort(((o1,o2)->o1.compareTo(o2)));

        list.forEach(s -> System.out.println(s));
    }

    public static void main5(String[] args) {
        PriorityQueue<Integer> maxHeap = new PriorityQueue<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o1.compareTo(o2);
            }
        });

        PriorityQueue<Integer> maxHeap2 = new PriorityQueue<>((x,y) ->{return x.compareTo(y);});
    }

    public static void main4(String[] args) {
        int a = 10;
        NoParameterNoReturn noParameterNoReturn = new NoParameterNoReturn(){
            //重写test方法
            public void test(){
                //此时捕获的变量一定是没有修改的，或者是一个常量
                System.out.println("a = " + a);
            }
        };


        NoParameterNoReturn noParameterNoReturn2 = () ->System.out.println("a ====>" +a);
        noParameterNoReturn2.test();
    }

    public static void main3(String[] args) {
        //NoParameterReturn noParameterReturn = () ->{return 10;};
        //优化
        NoParameterReturn noParameterReturn = () -> 10;
        int ret = noParameterReturn.test();
        System.out.println(ret);


        OneParameterReturn oneParameterReturn = (x) ->{return x;};
        int ret2 = oneParameterReturn.test(12);
        System.out.println(ret2);

        MoreParameterReturn moreParameterReturn =(x,y) -> {return x+y;};
        int ret3 = moreParameterReturn.test(10,20);
        System.out.println(ret3);

    }



    public static void main2(String[] args) {
        OneParameterNoReturn oneParameterReturn = x -> System.out.println(x);

        oneParameterReturn.test(10);
        System.out.println("========");

        MoreParameterNoReturn moreParameterNoReturn = (x,y) -> System.out.println(x+y);

        moreParameterNoReturn.test(10,20);

    }

    public static void main1(String[] args) {
        //匿名内部类
        //实现接口，重写test方法
        NoParameterNoReturn noParameterNoReturn = new NoParameterNoReturn(){
            //重写test方法
            public void test(){
                System.out.println("1234");
            }
        };

        noParameterNoReturn.test();
        System.out.println("=====================");

        /*() ->{
            System.out.println("1234");
        };*/


        NoParameterNoReturn noParameterNoReturn2 = () ->System.out.println("1234");
        noParameterNoReturn2.test();

    }
}
